package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.sycs.domain.SycsDataDetailSy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.SycsDataDetailWaterBo;
import com.ruoyi.sycs.domain.vo.SycsDataDetailWaterVo;
import com.ruoyi.sycs.domain.SycsDataDetailWater;
import com.ruoyi.sycs.mapper.SycsDataDetailWaterMapper;
import com.ruoyi.sycs.service.ISycsDataDetailWaterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验数据设置明细-水的密度Service业务层处理
 *
 * @author tll
 * @date 2024-04-09
 */
@RequiredArgsConstructor
@Service
public class SycsDataDetailWaterServiceImpl implements ISycsDataDetailWaterService {

    private final SycsDataDetailWaterMapper baseMapper;

    /**
     * 查询试验数据设置明细-水的密度
     */
    @Override
    public SycsDataDetailWaterVo queryById(Long deId){
        return baseMapper.selectVoById(deId);
    }

    /**
     * 查询试验数据设置明细-水的密度列表
     */
    @Override
    public TableDataInfo<SycsDataDetailWaterVo> queryPageList(SycsDataDetailWaterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SycsDataDetailWater> lqw = buildQueryWrapper(bo);
        Page<SycsDataDetailWaterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验数据设置明细-水的密度列表
     */
    @Override
    public List<SycsDataDetailWaterVo> queryList(SycsDataDetailWaterBo bo) {
        LambdaQueryWrapper<SycsDataDetailWater> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SycsDataDetailWater> buildQueryWrapper(SycsDataDetailWaterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SycsDataDetailWater> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTemp() != null, SycsDataDetailWater::getTemp, bo.getTemp());
        lqw.eq(bo.getDensity() != null, SycsDataDetailWater::getDensity, bo.getDensity());
        lqw.eq(bo.getCoefficient() != null, SycsDataDetailWater::getCoefficient, bo.getCoefficient());
        return lqw;
    }

    /**
     * 新增试验数据设置明细-水的密度
     */
    @Override
    public Boolean insertByBo(SycsDataDetailWaterBo bo) {
        SycsDataDetailWater add = BeanUtil.toBean(bo, SycsDataDetailWater.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeId(add.getDeId());
        }
        return flag;
    }

    /**
     * 修改试验数据设置明细-水的密度
     */
    @Override
    public Boolean updateByBo(SycsDataDetailWaterBo bo) {
        SycsDataDetailWater update = BeanUtil.toBean(bo, SycsDataDetailWater.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SycsDataDetailWater entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验数据设置明细-水的密度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 根据试验类型id获取明细
     * @param syTypeId
     * @return
     */
    @Override
    public List<SycsDataDetailWater> listBySyTypeId(Long syTypeId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("del_flag", "0");
        wrapper.eq("sy_type_id",syTypeId);
        List<SycsDataDetailWater> list = baseMapper.selectList(wrapper);
        return list;
    }
    @Override
    public Page listBySyTypeId(Long syTypeId, PageQuery pageQuery) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("del_flag", "0");
        wrapper.eq("sy_type_id",syTypeId);
//        wrapper.orderByAsc("sort");
        Page page = baseMapper.selectPage(pageQuery.build(), wrapper);
        return page;
    }
}
